#include "state_parser.h"
#include "game.h"
#include "game_object_factory.h"
#include "texture_manager.h"

bool StateParser::parseState(const char *stateFile, std::string stateID,
    std::vector<GameObject *> *pObjects, std::vector<std::string> *pTextureIDs)
{
    // create the XML document
    TiXmlDocument xmlDoc;
    
    // load the state file
    if(!xmlDoc.LoadFile(stateFile))
    {
        std::cout << xmlDoc.ErrorDesc() << "\n";
        return false;
    }
    
    // get the root element
    TiXmlElement* pRoot = xmlDoc.RootElement();
    
    // pre declare the states root node
    TiXmlElement* pStateRoot = NULL;
    // get this states root node and assing it to pStateRoot
    for(TiXmlElement* e = pRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
    {
        if(e->Value() == stateID)
        {
            pStateRoot = e;
			break;
        }
    }
    
    // pre declare the texture root
    TiXmlElement* pTextureRoot = NULL;
    
    // get the root of the texture elements
    for(TiXmlElement* e = pStateRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
    {
        if(e->Value() == std::string("TEXTURES"))
        {
            pTextureRoot = e;
			break;
        }
    }
    
    // now parse the textures
    parseTextures(pTextureRoot, pTextureIDs);
    
    // pre declare the object root node
    TiXmlElement* pObjectRoot = NULL;
    
    // get the root node and assign it to pObjectRoot
    for(TiXmlElement* e = pStateRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
    {
        if(e->Value() == std::string("OBJECTS"))
        {
            pObjectRoot = e;
			break;
        }
    }

    // now parse the objects
    parseObjects(pObjectRoot, pObjects);
    
    return true;
}

void StateParser::parseTextures(TiXmlElement* pStateRoot, std::vector<std::string> *pTextureIDs)
{
    // for each texture we get the filename and the desired ID and add them to the texture manager
    for(TiXmlElement* e = pStateRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
    {
        std::string filenameAttribute = e->Attribute("filename");
        std::string idAttribute = e->Attribute("ID");
        
        pTextureIDs->push_back(idAttribute); // push the id into the list
        
        TheTextureManager::Instance()->load(filenameAttribute, idAttribute, TheGame::Instance()->getRenderer());
    }
}

void StateParser::parseObjects(TiXmlElement *pStateRoot, std::vector<GameObject *> *pObjects)
{
    for(TiXmlElement* e = pStateRoot->FirstChildElement(); e != NULL; e = e->NextSiblingElement())
    {
        int x, y, width, height, numFrames, callbackID, animSpeed;
        double scale = 1;
        std::string textureID;
        
        e->Attribute("x", &x);
        e->Attribute("y", &y);
        e->Attribute("width",&width);
        e->Attribute("height", &height);
        e->Attribute("numFrames", &numFrames);
        e->Attribute("callbackID", &callbackID);
        e->Attribute("animSpeed", &animSpeed);
        e->Attribute("scale", &scale);
        
        textureID = e->Attribute("textureID");
        GameObject* pGameObject = TheGameObjectFactory::Instance()->create(e->Attribute("type"));
        pGameObject->load(std::unique_ptr<LoaderParams>(new LoaderParams(x, y,
            width, height, textureID, numFrames, callbackID, animSpeed, scale)));
        pObjects->push_back(pGameObject);
    }
}
